package com.huike.game.Activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.base.Game70re;

import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import androidx.appcompat.app.AppCompatActivity;

public class recodeActivity70 extends AppCompatActivity {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private TextView tv_game70_re_sum;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private ListView lv_re_70;
    private RequestQueue queue;
    private ProgressDialog show;
    private Game70re json;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recode70);
        queue = Volley.newRequestQueue(this);
        initView();
        initData();
    }

    private void initData() {
        show();
        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "ETCrecord", new Response.Listener<JSONObject>() {


            @Override
            public void onResponse(JSONObject jsonObject) {
                hide();

                json = new Gson().fromJson(jsonObject.toString(), Game70re.class);
                lv_re_70.setAdapter( new AdapterLists());

                //计算合计
                int sun = 0;
                for (int i = 0; i < json.getData().size(); i++) {
                    sun += json.getData().get(i).getRecharge();
                }
                tv_game70_re_sum.setText("充值合计:" + sun);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                hide();
                Toast.makeText(recodeActivity70.this, "网络异常！", Toast.LENGTH_SHORT).show();

            }
        });

        queue.add(request);
    }

    private void show(){
        show = ProgressDialog.show(this, "提示", "数据加载中...");
        show.show();
    }
    private void hide(){
        show.dismiss();
    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        tv_game70_re_sum = (TextView) findViewById(R.id.tv_game70_re_sum);
        lv_re_70 = findViewById(R.id.lv_re_70);


        tv_content_title.setText("充值记录");
        tv_left_text.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);
        iv_left_image.setImageResource(R.drawable.tiao);
        iv_rigth_image.setVisibility(View.GONE);
    }

    class AdapterLists extends BaseAdapter {

        @Override
        public int getCount() {

            return json.getData().size();
        }

        @Override
        public Object getItem(int position) {
            return json.getData().get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(recodeActivity70.this,R.layout.lv_recode_70,null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Game70re.DataBean bean = json.getData().get(position);
            Log.e("---",bean.getTime() +"---");
            //获取星期几
            String week = week(bean.getTime());
            holder.tv_game70_re_time.setText(bean.getTime() + "\n" + week);
            holder.tv_game70_re_numbser.setText(bean.getCarNumbser());
            holder.tv_game70_re_person.setText(bean.getPerson());
            holder.tv_game70_recharge_time.setText("充值时间\n" + bean.getTime());
            holder.tv_game70_re_price.setText("充值：" + bean.getRecharge() );

            return convertView;
        }

        //时间转换星期
        public String week(String time){
            SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
            Date date = null;
            try {
                 date = format.parse(time);//将时间解析为时间戳
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Calendar instance = Calendar.getInstance();
            instance.setTime(date);
            int i = instance.get(Calendar.DAY_OF_WEEK) - 1;
                if(i <= 0){
                    i = 0;
                }
            String[] strings = new String[]{"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};

                return strings[i];
        }

        class ViewHolder {
            public View rootView;
            public TextView tv_game70_re_time;
            public TextView tv_game70_re_person;
            public TextView tv_game70_re_numbser;
            public TextView tv_game70_re_price;
            public TextView tv_game70_recharge_time;

            public ViewHolder(View rootView) {
                this.rootView = rootView;
                this.tv_game70_re_time = (TextView) rootView.findViewById(R.id.tv_game70_re_time);
                this.tv_game70_re_person = (TextView) rootView.findViewById(R.id.tv_game70_re_person);
                this.tv_game70_re_numbser = (TextView) rootView.findViewById(R.id.tv_game70_re_numbser);
                this.tv_game70_re_price = (TextView) rootView.findViewById(R.id.tv_game70_re_price);
                this.tv_game70_recharge_time = (TextView) rootView.findViewById(R.id.tv_game70_recharge_time);
            }

        }
    }
}
